var express = require('express');
var router = express.Router();

const libQuery = (common, path = '/', type = 'json') => {
  // 一般是 get/post (我就都写了)
  router.get(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('get', path, type)
    common(req, res, next, 'get');
  });

  router.post(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('post', path, type)
    common(req, res, next, 'post')
  });

  router.put(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('put', path, type)
    common(req, res, next, 'put')
  });

  router.delete(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('delete', path, type)
    common(req, res, next, 'delete')
  });

  router.head(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('head', path, type)
    common(req, res, next, 'head')
  });

  router.purge(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('purge', path, type)
    common(req, res, next, 'purge')
  });

  router.lock(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('luck', path, type)
    common(req, res, next, 'lock')
  });

  router.unlock(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text = '', code = 500) {
      renderError(res, text, path, code)
    }
    printLog('unlock', path, type)
    common(req, res, next, 'unlock')
  });

  router.propfind(path, function (req, res, next) {
    type ? res.type(type) : res.type('json')
    res.renderError = function (text, code) {
      renderError(res, text, path, code)
    }
    printLog('propfind', path, type)
    common(req, res, next, 'propfind')
  });

  // 有问题
  // router.options(path, function (req, res, next) {
  //   type ? res.type(type) : res.type('json')
  //   console.log('options')
  //   common(req, res, next, 'options')
  // });

  // router.patch(path, function (req, res, next) {
  //   res.type('json');
  //   console.log('options')
  //   common(req, res, next, 'patch')
  // });
}

const libPathQuery = (name = '', path = '/') => {
  libQuery(function (req, res) {
    res.type('.html')
    res.render('path', { cache: false, title: name })
  }, path)
}

function renderError(res, text, path, code) {
  text = text || ''
  res.type('html')
  const error = new Error(text)
  res.locals.message = 'api.path= ' + path
  res.locals.error = error
  res.status(code || 500)
  res.render('error')
}

function printLog(method, path, type) {
  console.log('\n\n******* 新的请求 method=' + method + ' path=', path, '  return-type=', type)
}

module.exports = {
  router,
  libQuery,
  libPathQuery,
};